package com.xjrsoft.module.customerTwo.barin.barinFunctionalSubassembly.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaMode;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xjrsoft.common.page.PageOutput;
import com.xjrsoft.common.page.ConventPage;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import com.xjrsoft.common.utils.DataTransUtil;
import com.xjrsoft.common.result.Response;
import com.xjrsoft.core.secure.XjrUser;
import com.xjrsoft.core.secure.utils.SecureUtil;
import com.xjrsoft.core.tool.utils.BeanUtil;
import com.xjrsoft.module.base.entity.XjrBaseAnnexesFile;
import com.xjrsoft.module.base.service.IXjrBaseAnnexesFileService;
import com.xjrsoft.module.customerTwo.barin.barinFunctionalSubassembly.dto.TBarinFunctionalSubassemblyDto;
import com.xjrsoft.module.customerTwo.barin.barinFunctionalSubassembly.dto.SaveTBarinFunctionalSubassemblyFormDataDto;
import com.xjrsoft.module.customerTwo.barin.barinFunctionalSubassembly.vo.TBarinFunctionalSubassemblyMainVo;
import com.xjrsoft.module.customerTwo.barin.barinFunctionalSubassembly.vo.TBarinFunctionalSubassemblyVo;
import com.xjrsoft.module.customerTwo.barin.barinFunctionalSubassembly.vo.TBarinFunctionalSubassemblyListVo;
import com.xjrsoft.module.customerTwo.barin.barinFunctionalSubassembly.dto.TBarinFunctionalSubassemblyListDto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.xjrsoft.module.customerTwo.barin.barinFunctionalSubassembly.entity.TBarinFunctionalSubassembly;
import com.xjrsoft.module.customerTwo.barin.barinFunctionalSubassembly.service.ITBarinFunctionalSubassemblyService;

/**
 *  控制器
 *
 * @author dongxinhang
 * @since 2024-04-25
 */
@RestController
@AllArgsConstructor
@RequestMapping("/barinfunctionalsubassembly")
@Api(value = "功能组件", tags = "功能组件接口")
public class TBarinFunctionalSubassemblyController {


	private final ITBarinFunctionalSubassemblyService tBarinFunctionalSubassemblyService;
	@Autowired
	private IXjrBaseAnnexesFileService annexesFileService;

	/**
	 * 详情 
	 */
	@GetMapping("/{id}")
	@ApiOperation(value = "详情", notes = "传入tBarinFunctionalSubassembly")
	public Response<TBarinFunctionalSubassemblyVo> getFormData(@PathVariable String id) {
		// 主表数据
		TBarinFunctionalSubassembly tBarinFunctionalSubassembly = tBarinFunctionalSubassemblyService.getById(id);
		TBarinFunctionalSubassemblyVo tBarinFunctionalSubassemblyVo = BeanUtil.copy(tBarinFunctionalSubassembly, TBarinFunctionalSubassemblyVo.class);
		return Response.ok(tBarinFunctionalSubassemblyVo);
	}

	/**
	 * 自定义分页 
	 */
	@GetMapping
	@ApiOperation(value = "分页", notes = "传入tBarinFunctionalSubassembly")
		public Response<PageOutput<TBarinFunctionalSubassemblyListVo>> getPageList(TBarinFunctionalSubassemblyListDto listDto) {
		//如果是超级管理员查询所有数据
		XjrUser currentUser = SecureUtil.getUser();
		if(currentUser.isAdminUser()){
			listDto.setF_MerchantId("");
		}else {
			listDto.setF_MerchantId(currentUser.getMerchantid());
		}
		IPage<TBarinFunctionalSubassembly> page = tBarinFunctionalSubassemblyService.getPageList(listDto);
		List<TBarinFunctionalSubassemblyListVo> records = BeanUtil.copyList(page.getRecords(), TBarinFunctionalSubassemblyListVo.class);
			// 转换列表数据
		DataTransUtil.transListShowData(records);
		return Response.ok(ConventPage.getPageOutput(page.getTotal(), records));
	}

	/**
	 * 新增 
	 */
	@PostMapping
	@ApiOperation(value = "新增", notes = "传入tBarinFunctionalSubassembly")
	@SaCheckPermission("BarinFunctionalSubassembly:add")
	public Response save(@RequestBody SaveTBarinFunctionalSubassemblyFormDataDto formDto) {
		TBarinFunctionalSubassemblyDto tBarinFunctionalSubassemblyDto = formDto.getTBarinFunctionalSubassemblyDto();
		TBarinFunctionalSubassembly tBarinFunctionalSubassembly = BeanUtil.copy(tBarinFunctionalSubassemblyDto, TBarinFunctionalSubassembly.class);
		XjrUser currentUser = SecureUtil.getUser();
		tBarinFunctionalSubassembly.setFMerchantid(currentUser.getMerchantid());
		boolean isSuccess = tBarinFunctionalSubassemblyService.addTBarinFunctionalSubassembly(tBarinFunctionalSubassembly);
		return Response.status(isSuccess);
	}

	/**
	 * 修改 
	 */
	@PutMapping("/{id}")
	@ApiOperation(value = "修改", notes = "传入tBarinFunctionalSubassembly")
	@SaCheckPermission("BarinFunctionalSubassembly:modify")
	public Response update(@PathVariable String id, @RequestBody SaveTBarinFunctionalSubassemblyFormDataDto formDto) {
		TBarinFunctionalSubassemblyDto tBarinFunctionalSubassemblyDto = formDto.getTBarinFunctionalSubassemblyDto();
		TBarinFunctionalSubassembly tBarinFunctionalSubassembly = BeanUtil.copy(tBarinFunctionalSubassemblyDto, TBarinFunctionalSubassembly.class);
		return Response.status(tBarinFunctionalSubassemblyService.updateTBarinFunctionalSubassembly(id, tBarinFunctionalSubassembly));
	}



	/**
	 * 删除 
	 */
	@DeleteMapping("/{ids}")
	@ApiOperation(value = "删除", notes = "传入ids")
	@SaCheckPermission(value = {"BarinFunctionalSubassembly:batchDelete", "BarinFunctionalSubassembly:delete"}, mode = SaMode.OR)
	public Response remove(@ApiParam(value = "主键集合", required = true) @PathVariable String ids) {
		return Response.status(tBarinFunctionalSubassemblyService.delete(ids));
	}

	/**
	 * 自定义分页
	 */
	@GetMapping("/portalSearch")
	@ApiOperation(value = "分页", notes = "传入tBarinFunctionalSubassembly")
	public Response<PageOutput<TBarinFunctionalSubassemblyListVo>> portalSearch(TBarinFunctionalSubassemblyListDto listDto) {
		IPage<TBarinFunctionalSubassemblyListVo> page = tBarinFunctionalSubassemblyService.portalSearch(new Page<>(listDto.getLimit(), listDto.getSize()),listDto);
		List<TBarinFunctionalSubassemblyListVo> records = page.getRecords();
		// 转换列表数据
		DataTransUtil.transListShowData(records);
		return Response.ok(ConventPage.getPageOutput(page.getTotal(), records));
	}

	/**
	 * 详情
	 */
	@GetMapping("/mainInfo")
	@ApiOperation(value = "详情", notes = "传入tBarinFunctionalSubassembly")
	public Response<TBarinFunctionalSubassemblyMainVo> mainInfo(@RequestParam String id) {
		// 主表数据
		TBarinFunctionalSubassembly tBarinFunctionalSubassembly = tBarinFunctionalSubassemblyService.getById(id);
		TBarinFunctionalSubassemblyMainVo tBarinFunctionalSubassemblyVo = BeanUtil.copy(tBarinFunctionalSubassembly, TBarinFunctionalSubassemblyMainVo.class);
		//处理组件图标数据
		List<XjrBaseAnnexesFile> annexesFileList = annexesFileService.list(Wrappers.<XjrBaseAnnexesFile>query().lambda().eq(XjrBaseAnnexesFile::getFolderId, tBarinFunctionalSubassembly.getFIcon()));
		if(CollectionUtils.isNotEmpty(annexesFileList)){
			tBarinFunctionalSubassemblyVo.setFIcon(annexesFileList.get(0).getFilePath());
		}
		// 转换列表数据
		List<TBarinFunctionalSubassemblyMainVo> tempList = new ArrayList<>();
		tempList.add(tBarinFunctionalSubassemblyVo);
		DataTransUtil.transListShowData(tempList);
		return Response.ok(tempList.get(0));
	}

	/**
	 * 审核
	 */
	@PutMapping("/audit")
	@ApiOperation(value = "审核", notes = "传入tBrainMerchant")
	public Response audit(@RequestParam("id") String id,@RequestParam("status") String status) {
		TBarinFunctionalSubassembly tBarinFunctionalSubassembly = new TBarinFunctionalSubassembly();
		tBarinFunctionalSubassembly.setFStatus(status);
		if("3".equals(status)){		//操作上架，需要写入上架时间
			tBarinFunctionalSubassembly.setFSaledate(LocalDateTime.now());
		}
		return Response.status(tBarinFunctionalSubassemblyService.updateTBarinFunctionalSubassembly(id, tBarinFunctionalSubassembly));
	}
}
